package com.jxs.tourist.controller;

import com.jxs.tourist.domain.Tourist;
import com.jxs.tourist.service.TouristService;
import com.jxs.tourist.util.FileUpload;
import com.jxs.tourist.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/tourist")
public class TouristController {
    public TouristController(){
        System.out.println("init");
    }
    @Resource
    TouristService touristService;
    /**
     * 添加景点
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/addTourist")
    public JsonResult<String> addTourist(HttpServletRequest request){
        Tourist tourist=new Tourist();
        tourist.setAddress(request.getParameter("address"));
        tourist.setTourist_name(request.getParameter("name"));
        tourist.setLatitude(request.getParameter("latitude"));
        tourist.setLongitude(request.getParameter("longitude"));
        tourist.setCity_id(Integer.valueOf(request.getParameter("city_id")));
        tourist.setContent(request.getParameter("content"));
        tourist.setDescription(request.getParameter("description"));
        tourist.setOpendate(request.getParameter("opendate"));
        tourist.setOpentime(request.getParameter("opentime"));
        tourist.setCreatetime(new Date());
        tourist.setStar(request.getParameter("star"));
        tourist.setGallery(request.getParameter("imgs"));
        if(touristService.addTourist(tourist)==1){
            return new JsonResult<String>("","添加成功",0);
        }
        return new JsonResult<String>("","添加景点失败",1);
    }
    /**
     * 编辑景点
     * @param file
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/editTourist")
    public JsonResult<String> editTourist(HttpServletRequest request){
        int tourist_id=Integer.valueOf(request.getParameter("tourist_id"));
        Tourist tourist=touristService.getTouristById(tourist_id);
        if(tourist==null){
            return new JsonResult<String>("","不存在",1);
        }
        String name=request.getParameter("name");
        String city_id=request.getParameter("city_id");
        String longitude=request.getParameter("longitude");
        String latitude=request.getParameter("latitude");
        String address=request.getParameter("address");
        String description=request.getParameter("description");
        String star=request.getParameter("star");
        String content=request.getParameter("content");
        tourist.setAddress(address);
        tourist.setOpendate(request.getParameter("opendate"));
        tourist.setOpentime(request.getParameter("opentime"));
        tourist.setTourist_name(name);
        tourist.setLatitude(latitude);
        tourist.setLongitude(longitude);
        tourist.setGallery(request.getParameter("imgs"));
        tourist.setCity_id(Integer.valueOf(city_id));
        tourist.setContent(content);
        tourist.setDescription(description);
        tourist.setCreatetime(new Date());
        tourist.setStar(star);
        if(touristService.editTourist(tourist)==1){
            return new JsonResult<String>("","编辑成功",0);
        }
        return new JsonResult<String>("","编辑失败",1);
    }

    /**
     * 删除景点
     * @param tourist_id
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteTourist")
    public JsonResult<String> deleteTourist (@RequestParam("tourist_id") int tourist_id,HttpServletRequest request) {
        if(touristService.delTouristById(tourist_id)==1){
            return new JsonResult<String>("","删除成功",0);
        }
        return new JsonResult<String>("","删除失败",1);
    }
    /**
     * 查询所有景点后台
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllTourist",method = RequestMethod.POST)
    public JsonResult<List<Tourist>> allUser(HttpServletRequest request) {
        List<Tourist> list = touristService.getAllTourist();
        JsonResult<List<Tourist>> result;
        if (list == null || list.size() == 0) {
            result = new JsonResult<List<Tourist>>(list, "暂时没有数据", 1);
        } else {
            result = new JsonResult<List<Tourist>>(list, "获取成功", 0);
        }
        return result;
    }
    /**
     * 根据城市查找景点
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllTouristByCityId",method = RequestMethod.POST)
    public JsonResult<List<Tourist>> getAllTouristByCityId(HttpServletRequest request) {
        int city_id=Integer.valueOf(request.getParameter("city_id"));
        List<Tourist> list = touristService.getTouristByCityId(city_id);
        JsonResult<List<Tourist>> result;
        if (list == null || list.size() == 0) {
            result = new JsonResult<List<Tourist>>(list, "暂时没有数据", 1);
        } else {
            result = new JsonResult<List<Tourist>>(list, "获取成功", 0);
        }
        return result;
    }
    /**
     * 模糊查找
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchTourist",method = RequestMethod.POST)
    public JsonResult<List<Tourist>> searchTourist(HttpServletRequest request) {
        List<Tourist> list = touristService.searchTouristByName(request.getParameter("keyword"));
        JsonResult<List<Tourist>> result;
        if (list == null || list.size() == 0) {
            result = new JsonResult<List<Tourist>>(list, "暂时没有数据", 1);
        } else {
            result = new JsonResult<List<Tourist>>(list, "获取成功", 0);
        }
        return result;
    }
}
